package com.chaos.auth.service;

import cn.hutool.core.util.ObjectUtil;
import com.chaos.auth.api.dto.auth.RbacRoleMenuBtnDto;
import com.chaos.framework.model.dto.ID;
import com.chaos.framework.model.dto.page.PageList;
import com.chaos.framework.model.dto.page.PageQueryDto;
import com.chaos.framework.model.entity.Table;
import com.chaos.framework.model.helper.ConvertHelper;
import com.chaos.framework.model.helper.PageHelper;
import com.chaos.auth.entity.RbacRoleMenuBtn;
import com.chaos.auth.api.data.RbacRoleMenuBtnData;
import com.chaos.auth.mapper.RbacRoleMenuBtnMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Jacky.Cui
 * @since 2024-11-13
 */
@Slf4j
@Component
public class RbacRoleMenuBtnService extends ServiceImpl<RbacRoleMenuBtnMapper, RbacRoleMenuBtn> {

    public ID insertModel(RbacRoleMenuBtnData data) {
        RbacRoleMenuBtn entity = ConvertHelper.convert(data, RbacRoleMenuBtn.class);
        return save(entity) ? new ID(entity.getId()) : null;
    }

    public boolean deleteModel(ID data) {
        return removeById(data.getId());
    }

    public boolean updateModelByID(RbacRoleMenuBtnData data) {
        RbacRoleMenuBtn entity = ConvertHelper.convert(data, RbacRoleMenuBtn.class);
        return update(entity, new UpdateWrapper<RbacRoleMenuBtn>().eq(Table.ID, data.getId()));
    }

    public RbacRoleMenuBtnData selectByID(ID data) {
        return ConvertHelper.convert(getOne(new QueryWrapper<RbacRoleMenuBtn>().eq(Table.ID, data.getId())), RbacRoleMenuBtnData.class);
    }

    public List<RbacRoleMenuBtnData> selectByData(RbacRoleMenuBtnData data) {
        QueryWrapper<RbacRoleMenuBtn> query = new QueryWrapper<>();
        query.lambda()
                .eq(ObjectUtil.isEmpty(data.getRoleId()), RbacRoleMenuBtn::getRoleId, data.getRoleId())
                .eq(ObjectUtil.isEmpty(data.getCorrelationType()), RbacRoleMenuBtn::getCorrelationType, data.getCorrelationType())
        ;
        query.orderByDesc(Table.ID);
        return ConvertHelper.convert(list(query), RbacRoleMenuBtnData.class);
    }

    public PageList<RbacRoleMenuBtnData> selectByPage(PageQueryDto<RbacRoleMenuBtnData> pageData) {
        QueryWrapper<RbacRoleMenuBtn> query = new QueryWrapper<>();
        query.orderByDesc(Table.ID);
        return PageHelper.pageList(page(PageHelper.page(pageData), query), RbacRoleMenuBtnData.class);
    }

    public List<RbacRoleMenuBtnData> selectByData(RbacRoleMenuBtnDto data) {
        QueryWrapper<RbacRoleMenuBtn> query = new QueryWrapper<>();
        query.lambda()
                .eq(ObjectUtil.isNotEmpty(data.getRoleId()), RbacRoleMenuBtn::getRoleId, data.getRoleId())
                .eq(ObjectUtil.isNotEmpty(data.getCorrelationType()), RbacRoleMenuBtn::getCorrelationType, data.getCorrelationType())
                .in(ObjectUtil.isNotEmpty(data.getRoleIds()), RbacRoleMenuBtn::getRoleId, data.getRoleIds())
        ;
        query.orderByDesc(Table.ID);
        return ConvertHelper.convert(list(query), RbacRoleMenuBtnData.class);
    }

}
